<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--盘点结果录入模态框-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">盘点结果录入</h4>
            </div>
            <div class="modal-body">
                <form id="checkDetailForm" class="form-horizontal" role="form">
                    <input type="hidden" id="cdlId" name="cdlId">
                    <input type="hidden" id="matSpec">
                    <input type="hidden" id="matLength">
                    <input type="hidden" id="matWidth">
                    <input type="hidden" id="matHeight">
                    <input type="hidden" id="formulaType">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">物料描述</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="matNameDesc"
                                   readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">盘点数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="cdlNum" name="cdlNum"
                                   autocomplete="off" placeholder="请输入盘点数量">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">盘点重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="cdlWeight" name="cdlWeight"
                                   autocomplete="off" placeholder="请输入盘点重量">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-commit">确定</button>
            </div>
        </div>
    </div>
</div>

<!--新增盘点模态框-->
<div class="modal fade" id="modal-newCheckDetail">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">新增盘点录入</h4>
            </div>
            <div class="modal-body">
                <form id="newCheckDetailForm" class="form-horizontal" role="form">
                    <input type="hidden" id="cdlCpnIdNew" name="cdlCpnId">
                    <input type="hidden" id="cdlMatIdNew" name="cdlMatId">
                    <input type="hidden" id="matSpecNew">
                    <input type="hidden" id="matLengthNew">
                    <input type="hidden" id="matWidthNew">
                    <input type="hidden" id="matHeightNew">
                    <input type="hidden" id="formulaTypeNew">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">原料描述</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" id="matNameDescNew"
                                   readonly="readonly">
                        </div>
                        <div class="col-sm-1">
                            <button type="button" id="btn-chooseMaterial"
                                    class="btn btn-info pull-right">
                                <span class="glyphicon glyphicon-search"></span>选 择
                            </button>
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">单价</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="cdlPriceNew" name="cdlPrice"
                                   autocomplete="off" placeholder="请输入单价">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">盘点数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="cdlNumNew" name="cdlNum"
                                   autocomplete="off" placeholder="请输入盘点数量">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">盘点重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="cdlWeightNew" name="cdlWeight"
                                   autocomplete="off" placeholder="请输入盘点重量">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-saveNewCheckDetail">确定</button>
            </div>
        </div>
    </div>
</div>

<!--原料基础信息模态框-->
<div class="modal fade" id="modal-chooseMaterial" tabindex="-1" data-keyboard="true">
    <div class="modal-dialog" style="width: 1350px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">原料基础信息</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header">
                                <form id="queryForm">
                                    <div class="row">
                                        <div class="form-inline">
                                            <div class="form-group">
                                                <label>原料分类</label>
                                                <select name="materialKind"
                                                        class="form-control">
                                                    <option value="板材">板材</option>
                                                    <option value="型材">型材</option>
                                                    <option value="" selected="selected">全部</option>
                                                </select>
                                            </div>
                                            <div class="form-group">
                                                <label>类型</label>
                                                <input type="text" name="matTypeName"
                                                       class="form-control"
                                                       placeholder="请输入原料类型">
                                            </div>
                                            <div class="form-group">
                                                <label>材质</label>
                                                <input type="text" name="matQualityName"
                                                       class="form-control"
                                                       placeholder="请输入原料材质">
                                            </div>
                                            <div class="form-group">
                                                <label>规格</label>
                                                <input type="text" name="matSpecName"
                                                       class="form-control"
                                                       placeholder="请输入原料规格">
                                            </div>
                                            <div class="form-group">
                                                <label>宽度</label>
                                                <input type="number" step="0.1" name="matWidth" class="form-control"
                                                       placeholder="请输入原料宽度">
                                            </div>
                                            <div class="form-group">
                                                <label>长度</label>
                                                <input type="number" step="0.1" name="matLength" class="form-control"
                                                       placeholder="请输入原料长度">
                                            </div>
                                            <div class="form-group">
                                                <label>米重/厚度</label>
                                                <input type="number" step="0.1" name="matHeight" class="form-control"
                                                       placeholder="请输入原料米重/厚度">
                                            </div>
                                            <button type="button" class="btn btn-success" id="btn_materialSearch">搜索
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="box-body">
                                <table id="bootstrap-materials-table">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<section class="content-header">
    <h1>盘点结果录入</h1>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a th:href="@{'/material/checkPlan/adjust/'+${checkPlan.cpnId}}"
           shiro:hasPermission="matinv:matcheckadj:view" id="btn-next" th:if="${checkPlan.cpnResStatus==3}"
           class="btn btn-warning">下一步
        </a>
    </div>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a href="/material/checkPlan/plan" shiro:hasPermission="matinv:matcheckplan:view"
           id="btn-return-checkPlanList"
           class="btn btn-info">返回计划列表
        </a>
    </div>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div class="row" style="margin-bottom: 10px">
                        <div class="form-inline">
                            <div class="form-group" style="margin-right: 10px;">
                                <label>
                                    计划编号
                                </label>
                                <input type="text" class="form-control" th:value="${checkPlan.cpnCode}"
                                       readonly="readonly">
                            </div>
                            <div class="form-group" style="margin-right: 10px;">
                                <label>
                                    盘点内容
                                </label>
                                <input type="text" class="form-control" th:value="${checkPlan.cpnContent}"
                                       readonly="readonly">
                            </div>

                            <input type="hidden" id="cpnId" name="cpnId"
                                   th:value="${checkPlan.cpnId}">
                            <input type="hidden" id="cpnResStatus"
                                   th:value="${checkPlan.cpnResStatus}">
                        </div>
                    </div>
                </div>
                <div class="row" id="toolbar1">
                    <button shiro:hasPermission="matinv:matcheckresult:edit" id="btn-newCheckDetail"
                            class="btn btn-info"
                            th:style="'display:' + @{(${checkPlan.cpnResStatus} == 3 ? 'none;' : 'inline-block;')}">
                        新增盘点
                    </button>
                    <a id="export" shiro:hasPermission="matinv:matcheckresult:edit"
                       th:href="@{/material/checkPlan/exportCheckDetail}"
                       class="btn btn-primary"
                       th:style="'display:' + @{(${checkPlan.cpnResStatus} == 3 ? 'none;' : 'inline-block;')}">导出盘点计划</a>
                    <a id="import" shiro:hasPermission="matinv:matcheckresult:edit"
                       th:href="@{/material/checkPlan/import}"
                       class="btn btn-info"
                       th:style="'display:' + @{(${checkPlan.cpnResStatus} == 3 ? 'none;' : 'inline-block;')}">导入盘点结果</a>
                    <button shiro:hasPermission="matinv:matcheckresult:edit" id="clear"
                            class="btn btn-danger"
                            th:style="'display:' + @{(${checkPlan.cpnResStatus} == 3 ? 'none;' : 'inline-block;')}">
                        清空盘点结果
                    </button>
                    <button shiro:hasPermission="matinv:matcheckresult:edit" id="btn-updateResStatus"
                            class="btn btn-success"
                            th:text="@{(${checkPlan.cpnResStatus} == 3 ? '撤销盘点结果' : '提交盘点结果')}"
                            th:style="'display:' + @{(${checkPlan.cpnAdjStatus} == 3 ? 'none;' : 'inline-block;')}">
                    </button>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var url = $("#export").attr("href");
    var urr = $("#import").attr("href");

    /** 加载表格 */
    var table1, table2;

    /** 录入盘点结果模态框（单条） */
    function update(id, d) {
        $("#cdlId").val(id);
        $("#matNameDesc").val(d.matNameDesc);
        $("#matSpec").val(d.matSpecName);
        $("#matLength").val(d.matLength);
        $("#matWidth").val(d.matWidth);
        $("#matHeight").val(d.matHeight);
        $("#formulaType").val(d.matFormulaType);

        js.modal.open("modal-create");
    }

    /** 获取物料信息（模态框数据源）*/
    function openMaterialList() {
        table2 = js.table.init({
            id: "bootstrap-materials-table",
            url: ctx + "basic/materials/list",
            pageSize: 100,
            showExport: false,
            columns: [
                {
                    title: '序号', field: '', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '编码', field: 'matCode'},
                {title: '分类', field: 'materialKind'},
                {title: '类型', field: 'matTypeName'},
                {title: '材质', field: 'matQualityName'},
                {title: '规格', field: 'matSpecName'},
                {title: '宽度', field: 'matWidth'},
                {title: '长度', field: 'matLength'},
                {title: '米重/厚度', field: 'matHeight'},
                {
                    title: '选择', width: '80',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push("<a class='btn btn-info btn-xs' href='javascript:void(0);' onclick='getMaterial(" + JSON.stringify(row) + " )'>选择</a>");
                        return actions.join('');
                    }
                }
            ]
        });
    }

    /** 取得原料行数据 */
    function getMaterial(d) {
        $("#cdlCpnIdNew").val($("#cpnId").val());

        $("#cdlMatIdNew").val(d.matId);
        $("#matSpecNew").val(d.matSpecName);
        $("#matLengthNew").val(d.matLength);
        $("#matWidthNew").val(d.matWidth);
        $("#matHeightNew").val(d.matHeight);
        $("#formulaTypeNew").val(d.matFormulaType);

        $("#matNameDescNew").val(d.matNameDesc);

        // 关闭模态框
        js.modal.hide("modal-chooseMaterial");
    }

    $(function () {

        /** 导出盘点计划 */
        $("#export").click(function () {
            var cpnId = $("#cpnId").val();
            var href = url + "?cpnId=" + cpnId
            $(this).attr("href", href);
        });

        /** 导入盘点结果 */
        $("#import").click(function () {
            var cpnId = $("#cpnId").val();
            var href = urr + "?cpnId=" + cpnId
            $(this).attr("href", href);
        });

        /** 加载列表 */
        table1 = js.table.init({
            url: ctx + "material/checkPlan/planDetailList",
            pageSize: 300,
            showExport: false,
            toolbar: 'toolbar1',
            queryData: {cpnId: $("#cpnId").val()},
            columns: [
                {
                    title: '序号', field: '', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '类型', field: 'matTypeName'},
                {title: '材质', field: 'matQualityName'},
                {title: '规格', field: 'matSpecName'},
                {title: '宽度', field: 'matWidth'},
                {title: '长度', field: 'matLength'},
                {title: '米重/厚度', field: 'matHeight'},
                {title: '在库总数', field: 'cdlBeforeNum'},
                {title: '在库总量', field: 'cdlBeforeWeight'},
                {title: '库存在库数', field: 'cdlItyNum'},
                {title: '库存在库量', field: 'cdlItyWeight'},
                {title: '盘点数量', field: 'cdlNum'},
                {
                    title: '盘点重量', field: 'cdlWeight',
                    formatter: function (value, row, index) {
                        if (row.cdlStatus === 2 && row.matFormulaType == null && value == null) {
                            return '<span class="label label-danger">无运算公式</span>';
                        } else {
                            return value;
                        }
                    }
                },
                {title: '盘点重量损溢差', field: 'differenceValue'},
                {
                    title: '盘点状态', field: 'cdlStatus',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '未盘点';
                        } else if (value === 2) {
                            return '已盘点';
                        }
                    }
                },
                {
                    title: '操作', width: '80',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("matinv:matcheckresult:edit") && row.cpnResStatus != 3) {
                            actions.push("<a class='btn btn-warning btn-xs' href='javascript:void(0);' onclick='update(" + row.cdlId + "," + JSON.stringify(row) + ")'>盘点</a>");
                        }
                        return actions.join('');
                    }
                }
            ],
            rowStyle: function (row, index) {
                if (row.cdlStatus === 2) {
                    return {css: {"background": "#00a65a"}}
                } else {
                    return ''
                }
            }
        });

        /**关闭录入盘点结果模态框*/
        js.modal.hideEvent("modal-create", function () {
            js.reset("checkDetailForm");
        });

        /** 盘点数量变更 联动盘点重量 */
        $("#cdlNum").change(function () {

            var spec = $("#matSpec").val();
            var length = $("#matLength").val();
            var width = $("#matWidth").val();
            var height = $("#matHeight").val();
            var cdlNum = $("#cdlNum").val();
            var formulaType = $("#formulaType").val();

            var cdlWeight = returnMaterialWeight(spec, length, width, height, cdlNum, formulaType);
            $("#cdlWeight").val(cdlWeight);
        });

        /** 保存盘点结果 */
        $("#btn-commit").click(function () {

            js.validSubmit({
                formId: "checkDetailForm",
                url: ctx + "material/checkPlan/checkResultSingle",
                data: new FormData($("#checkDetailForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.hide("modal-create");
                        js.modal.success("操作成功");
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 清空盘点结果 */
        $("#clear").click(function () {
            js.modal.confirm("确定清空盘点结果吗？", function (result) {
                js.post({
                    url: ctx + "material/checkPlan/clear",
                    data: {
                        cpnId: $("#cpnId").val()
                    },
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table1);
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })
            })
        });
        /** 更新盘点状态 */
        $("#btn-updateResStatus").click(function () {
            var msg;
            if ($("#cpnResStatus").val() == 3) {
                msg = "确定撤销盘点结果吗？撤销后，本次盘点损益调整记录将自动清除！"
            } else {
                msg = "确定提交盘点结果吗?"
            }
            js.modal.confirm(msg, function (result) {
                js.post({
                    url: ctx + "material/checkPlan/updateResStatus",
                    data: {
                        cpnId: $("#cpnId").val()
                    },
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            location.reload();
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })
            })
        });

        /** 打开新增盘点模态框 */
        $("#btn-newCheckDetail").on('click', function () {
            js.modal.open("modal-newCheckDetail")
        });

        /** 关闭新增盘点模态框 */
        js.modal.hideEvent("modal-newCheckDetail", function () {
            js.reset("newCheckDetailForm");
        });

        /** 盘点数量变更 联动盘点重量 */
        $("#cdlNumNew").change(function () {

            var spec = $("#matSpecNew").val();
            var length = $("#matLengthNew").val();
            var width = $("#matWidthNew").val();
            var height = $("#matHeightNew").val();
            var cdlNum = $("#cdlNumNew").val();
            var formulaType = $("#formulaTypeNew").val();

            var cdlWeight = returnMaterialWeight(spec, length, width, height, cdlNum, formulaType);
            $("#cdlWeightNew").val(cdlWeight);
        });

        /** 打开物料模态框 */
        $("#btn-chooseMaterial").on('click', function () {
            openMaterialList();
            js.modal.open("modal-chooseMaterial")
        });

        /** 表格搜索原料 */
        $("#btn_materialSearch").click(function () {
            js.table.search(table2);
        });

        /** 关闭物料模态框 */
        js.modal.hideEvent("modal-chooseMaterial", function () {
            js.table.destroy(table2);
            js.reset("queryForm");
        });

        /** 保存新增盘点 */
        $("#btn-saveNewCheckDetail").click(function () {

            js.validSubmit({
                formId: "newCheckDetailForm",
                url: ctx + "material/checkPlan/saveNewCheckDetail",
                data: new FormData($("#newCheckDetailForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.hide("modal-newCheckDetail");
                        js.modal.success("操作成功");
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

    })

</script>

</body>
</html>